﻿using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DotNetChatServer.Interfaces;
using Contract.Data;
using System.Collections.Generic;
using System;

namespace DotNetChatServer {
	public class SimpleMemberStorage : IChatMemberCollection {
		List<ChatMember> myLogins = new List<ChatMember>();
		#region ChatMembers Members

		public ChatMember CreateLogin(string user, int avatar) {
			// if the username is taken then dont allow the login ot be created
			if ((from testMember in myLogins where user.Equals(testMember.Name) select 1).Count<int>() > 0)
				throw new Exception("Login already in use");

			ChatMember member = new ChatMember() {
				Avatar = avatar,
				Name = user,
				password = new Guid().ToString().Remove(8),
				IsAdmin = false,
				CreatedTime = DateTime.Now,
				LastActivityTime = DateTime.Now
			};
			myLogins.Add(member);
			return member;
		}

		public ChatMember DoLogin(string user, string password) {
			var retMember = (from member in myLogins
											 where member.password != null && member.Name != null && member.password.Equals(password) && member.Name.Equals(user)
											 select member).FirstOrDefault<ChatMember>();
			if (retMember == null)
				retMember = CreateLogin(user, 0);
			return retMember;

		}

		public void DeleteLogin(ChatMember member) {
			myLogins.Remove(member);
		}

		public void Logout(ChatMember member) {
			DeleteLogin(member);
		}

		#endregion
	}
}
